# -*- coding:utf-8 -*-

import os
import time
import logging
import logging.handlers

#默认输出格式
DEFAULT_FORMATTER = "%(levelname)s[%(asctime)s]: %(message)s" 
#handler类型
SIZEROTATE = 0
TIMEROTATE = 1
#默认日志大小
DEFAULT_SIZE = 100*1024*1024
#默认时间间隔
DEFAULT_TIME = 1
#默认按大小备份数
DEFAULT_SIZE_COUNT = 3
#默认按时间备份数
DEFAULT_TIME_COUNT = 3

#class MyLogger(logging.getLoggerClass()):
#    def __init__(self, file_name, hdlr_type=SIZEROTATE, fmt=DEFAULT_FORMATTER):
#        logging.getLoggerClass().__init__(self, file_name)
#        if hdlr_type == SIZEROTATE:
#            handler = logging.handlers.RotatingFileHandler(os.path.join("../log", filename), "a", DEFAULT_SIZE, DEFAULT_SIZE_COUNT)
#        elif hdlr_type == TIMEROTATE:
#            handler = logging.handlers.TimedRotatingFileHandler(os.path.join("../log", filename), "D", DEFAULT_TIME, DEFAULT_TIME_COUNT)
#        else:
#            raise Exception("handler type error")
#        handler = logging.FileHandler(os.path.join("../log", filename))
#        formatter = logging.Formatter(DEFAULT_FORMATTER)
#        handler.setFormatter(formatter)
#        self.addHandler(handler)
#        self.setLevel(logging.DEBUG)

#日志文件记录
logger_list = []

class MyLogger:
    def __init__(self, file_name, hdlr_type=SIZEROTATE, bak_num=DEFAULT_SIZE_COUNT):
        """
        初始化
        """
        if file_name in logger_list:
            self.logger = self.logger = logging.getLogger(file_name)
        else:
            formatter = logging.Formatter(DEFAULT_FORMATTER)
            if hdlr_type == SIZEROTATE:
                handler = logging.handlers.RotatingFileHandler(file_name, "a", DEFAULT_SIZE, bak_num)
            elif hdlr_type == TIMEROTATE:
                handler = logging.handlers.TimedRotatingFileHandler(file_name, "D", DEFAULT_TIME, bak_num)
            else:
                raise Exception("handler type error")
            handler.setFormatter(formatter)
            self.logger = logging.getLogger(file_name)
            self.logger.addHandler(handler)
            self.logger.setLevel(logging.DEBUG)
            logger_list.append(file_name)
            
    def info(self, msg):
        """
        记录日志，日志级别：INFO
        """
        self.logger.info(msg)
    
    def error(self, msg):
        """
        记录日志，日志级别：ERROR
        """
        self.logger.error(msg)
        
    def debug(self, msg):
        """
        记录日志，日志级别：DEBUG
        """
        self.logger.debug(msg)
        
    def warning(self, msg):
        """
        记录日志，日志级别：WARNING
        """
        self.logger.warning(msg)
        
    def exception(self, msg):
        """
        记录日志，同时记录异常信息，日志级别：ERROR
        """
        self.logger.exception(msg)
    def set_log_lev(self, lev):
        """
        设置日志等级
        """
        if (lev == 'ERROR'):
            self.logger.setLevel(logging.ERROR)
        elif( lev == 'WARNING'):
            self.logger.setLevel(logging.WARNING)
        elif (lev == 'DEBUG'):
            self.logger.setLevel(logging.DEBUG)
        elif (lev == 'INFO'):
            self.logger.setLevel(logging.INFO)

#if __name__ == "__main__":
#    logger1 = MyLogger("test1.log", SIZEROTATE)
#    logger2 = MyLogger("test2.log", TIMEROTATE)
#    while True:
#        logger1.info("111111111111111")
#        logger2.info("222222222222222")
#    try:
#        raise Exception("error")
#    except:
#        logger1.exception("unknown error")
 